package com.wwu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwu.common.utils.AssertUtil;
import com.wwu.common.utils.PinyinUtils;
import com.wwu.common.web.ResultPage;
import com.wwu.system.entity.SysDictType;
import com.wwu.system.mapper.SysDictTypeMapper;
import com.wwu.system.service.ISysDictService;
import com.wwu.system.service.ISysDictTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;

/**
 *  系统字典类型服务实现类
 * @author 一蓑烟雨
 * @date 2023-07-03
 * @version 1.0.0
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {
    @Resource
    private ISysDictService sysDictService;

    /**
     * 根据字典类型编码或名称获取字典类型
     * @param itemTypeId   字典类型编码
     * @param itemTypeName  字典类型名称
     * @return com.wwu.system.entity.SysDictType
     * @author 一蓑烟雨
     * @date 2023/7/3 20:01
     */
    @Override
    public SysDictType getDictTypeByIdOrName(String dictTypeId, String dictTypeName) {
        LambdaQueryWrapper<SysDictType> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasText(dictTypeId)){
            wrapper.eq(SysDictType::getDictTypeId,dictTypeId);
        }
        if(StringUtils.hasText(dictTypeName)){
            wrapper.eq(SysDictType::getDictTypeName,dictTypeName);
        }
        return this.getOne(wrapper);
    }

    /**
     * 分页查询系统字典类型信息
     * @param pageNum  页码
     * @param pageSize  每页数量
     * @param sysDictType  字典类型实体信息
     * @return com.wwu.common.web.ResultPage<com.wwu.system.entity.SysDictType>
     * @author 一蓑烟雨
     * @date 2023/7/3 18:08
     */
    @Override
    public ResultPage<SysDictType> getDictTypeListPage(Integer pageNum, Integer pageSize, SysDictType sysDictType) {
        Page<SysDictType> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SysDictType> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasText(sysDictType.getDictTypeName())){
            wrapper.like(SysDictType::getDictTypeName,sysDictType.getDictTypeName());
        }
        if(null != sysDictType.getDictTypeStatus()){
            wrapper.eq(SysDictType::getDictTypeStatus,sysDictType.getDictTypeStatus());
        }
        wrapper.orderByAsc(SysDictType::getDictTypeSort);
        IPage<SysDictType> sysDictTypePage = this.page(page, wrapper);
        return new ResultPage<>(sysDictTypePage);
    }

    /**
     * 添加字典类型信息
     * @param sysDictType  字典类型实体信息
     * @author 一蓑烟雨
     * @date 2023/7/3 18:08
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveDictType(SysDictType sysDictType) {
        AssertUtil.isTrue(!StringUtils.hasText(sysDictType.getDictTypeId()),"类型编码不能为空");
        SysDictType sysDictTypeTemp = this.getDictTypeByIdOrName(sysDictType.getDictTypeId(),null);
        AssertUtil.isTrue(null != sysDictTypeTemp,"类型编码已存在");

        AssertUtil.isTrue(!StringUtils.hasText(sysDictType.getDictTypeName()),"类型名称不能为空");
        SysDictType sysDictTypeTemp1 = this.getDictTypeByIdOrName(null,sysDictType.getDictTypeName());
        AssertUtil.isTrue(null != sysDictTypeTemp1 && !sysDictType.getDictTypeId().equals(sysDictTypeTemp1.getDictTypeId()),"类型名称已存在");

        sysDictType.setPy(PinyinUtils.getPinYin(sysDictType.getDictTypeName()));
        sysDictType.setWb(PinyinUtils.getWuBi(sysDictType.getDictTypeName()));
        AssertUtil.isTrue(!(this.save(sysDictType)),"字典类型添加失败！");
    }

    /**
     * 更新字典类型信息
     * @param sysDictType  字典类型实体信息
     * @author 一蓑烟雨
     * @date 2023/7/3 18:08
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateDictType(SysDictType sysDictType) {
        AssertUtil.isTrue(!StringUtils.hasText(sysDictType.getDictTypeId()),"类型编码不能为空");
        SysDictType sysDictTypeTemp = this.getDictTypeByIdOrName(sysDictType.getDictTypeId(),null);
        AssertUtil.isTrue(null != sysDictTypeTemp && !sysDictType.getDictTypeId().equals(sysDictTypeTemp.getDictTypeId()),"类型编码已存在");

        AssertUtil.isTrue(!StringUtils.hasText(sysDictType.getDictTypeName()),"类型名称不能为空");
        SysDictType sysDictTypeTemp1 = this.getDictTypeByIdOrName(null,sysDictType.getDictTypeName());
        AssertUtil.isTrue(null != sysDictTypeTemp1 && !sysDictType.getDictTypeId().equals(sysDictTypeTemp1.getDictTypeId()),"类型名称已存在");

        sysDictType.setPy(PinyinUtils.getPinYin(sysDictType.getDictTypeName()));
        sysDictType.setWb(PinyinUtils.getWuBi(sysDictType.getDictTypeName()));
        AssertUtil.isTrue(!(this.updateById(sysDictType)),"字典类型更新失败！");
    }

    /**
     * 更新字典类型状态
     * @param dictTypeId 字典类型编码
     * @param dictTypeStatus 字典类型状态
     * @author 一蓑烟雨
     * @date 2023/7/3 18:09
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateDictTypeStatus(String dictTypeId, Integer dictTypeStatus) {
        AssertUtil.isTrue(!StringUtils.hasText(dictTypeId),"类型编码不能为空");
        AssertUtil.isTrue(ObjectUtils.isEmpty(dictTypeStatus),"类型状态不能为空");

        LambdaUpdateWrapper<SysDictType> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(SysDictType::getDictTypeStatus,dictTypeStatus)
               .eq(SysDictType::getDictTypeId,dictTypeId);
        AssertUtil.isTrue(!this.update(wrapper),"字典类型状态更新失败！");
    }

    /**
     * 删除字典类型(逻辑删除)
     * @param dictTypeIds 字典类型编码集合
     * @author 一蓑烟雨
     * @date 2023/7/3 18:09
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteDictType(List<String> dictTypeIds) {
        AssertUtil.isTrue(ObjectUtils.isEmpty(dictTypeIds),"请选择删除的记录！");
        //删除字典明细信息
        sysDictService.deleteDictByTypeId(dictTypeIds);
        //删除字典类型信息
        AssertUtil.isTrue(!this.removeByIds(dictTypeIds),"字典类型及其明细删除失败");
    }
}
